package com.mycompany.sigemp.repository.hibernate;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.mycompany.sigemp.model.Cliente;
import com.mycompany.sigemp.repository.ClienteRepository;

@Repository
public class HibernateClienteRepository extends HibernateDaoSupport implements ClienteRepository {
	
	@Autowired
	public HibernateClienteRepository(SessionFactory sessionFactory){
		setSessionFactory(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Cliente obtenerClientePorCodigo(String codigo) {
		List<Cliente> lista = getHibernateTemplate().find("from Cliente where codigo='"+codigo+"'");
		if(lista.size()==0)
			return null;
			return lista.get(0);
	}

	@Override
	public void resgistrarUnCliente(Cliente c) {
		getHibernateTemplate().saveOrUpdate(c);
	}

	@Override
	public void actualizarCliente(Cliente c) {
		getHibernateTemplate().update(c);
	}

	@Override
	public void eliminarUnCliente(Cliente c) {
		getHibernateTemplate().delete(c);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Cliente> obtenerTodosLosClientes() {
		return getHibernateTemplate().find("from Cliente");
	}

}
